package org.my.business.controller.app.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.BCrypt;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.my.business.controller.app.user.vo.AppUserCreateVO;
import org.my.common.core.domain.R;
import org.my.common.core.utils.MapstructUtils;
import org.my.common.core.utils.StringUtils;
import org.my.common.log.annotation.Log;
import org.my.common.log.enums.BusinessType;
import org.my.common.mybatis.core.page.PageQuery;
import org.my.common.mybatis.core.page.TableDataInfo;
import org.my.common.satoken.utils.LoginHelper;
import org.my.common.tenant.helper.TenantHelper;
import org.my.common.web.core.BaseController;
import org.my.system.domain.bo.SysUserBo;
import org.my.system.domain.vo.SysUserVo;
import org.my.system.service.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * app用户管理
 * @author JohnnyKang
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/business/user")
public class AppUserController extends BaseController {

    private final ISysUserService userService;
    private final ISysDeptService deptService;
    private final ISysTenantService tenantService;
    private final ISysRoleService roleService;

    /**
     * 新增app员工
     */
    @Log(title = "app员工管理", businessType = BusinessType.INSERT)
    @PostMapping("/createEmp")
    public R<Void> add(@Validated @RequestBody AppUserCreateVO createVO) {
        SysUserBo user = MapstructUtils.convert(createVO, SysUserBo.class);
        user.setDeptId(LoginHelper.getDeptId());
        user.setUserType("app_user");
        user.setPassword(BCrypt.hashpw("123456"));

        //获取当前租户下员工角色id
        Long[] roleIds = roleService.getAppUserRoleIds(LoginHelper.getTenantId(),"16".equals(createVO.getCardType()));
        user.setRoleIds(roleIds);

        deptService.checkDeptDataScope(user.getDeptId());
        if (!userService.checkUserNameUnique(user)) {
            return R.fail("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) {
            return R.fail("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) {
            return R.fail("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        if (TenantHelper.isEnable()) {
            if (!tenantService.checkAccountBalance(TenantHelper.getTenantId())) {
                return R.fail("当前租户下用户名额不足，请联系管理员");
            }
        }

        return toAjax(userService.insertUser(user));
    }


    /**
     * 获取用户列表
     */
    @GetMapping("/listAppUser")
    public TableDataInfo<SysUserVo> listAppUser(SysUserBo user, PageQuery pageQuery) {
        return userService.selectPageUserList(user, pageQuery);
    }
}
